using System;
using System.Drawing;
using TTDotNet.Util;
using TTDotNet.Engine.Vehicles;
using TTDotNet.Engine.GUI.Buttons;
using TTDotNet.Engine.Create.Tools;
using TTDotNet.Engine.Stations;
using Microsoft.DirectX.Direct3D;

namespace TTDotNet.Engine.GUI.Windows
{
    public class OrdersWindow : Window
    {
        private Vehicle vehc;
        private int listboxNum;
        private int gotoBox;

        public OrdersWindow(Vehicle vehicle, PointF screenLocation)
        {
            this.vehc = vehicle;

            int cSize = 24;
            int border = 2;
            int bWidth = 64;

            MinimumSize = new SizeF((7 * bWidth) + cSize + (9 * border), 200);

            float width = MinimumSize.Width;
            float height = MinimumSize.Height;

            SetScreenRectangle(new RectangleF(screenLocation, MinimumSize));

            int controlNum = 0;

            windowControls[controlNum++] = new Label(new RectangleF(new PointF(0, 0), new SizeF(width, height)), "", 1, Color.Black, "Arial",
                                                        Game.textureCacher.LoadTexture("artwork/colours/black.jpg"), DrawTextFormat.Center | DrawTextFormat.VerticalCenter);

            windowControls[controlNum++] = new CloseButton(new RectangleF(new PointF(border, border), new SizeF(cSize, cSize)),
                                            Game.textureCacher.LoadTexture("artwork/128/close.jpg"), Game.textureCacher.LoadTexture("artwork/128/closeDown.jpg"), this);

            windowControls[controlNum++] = new TitleBar(new RectangleF(new PointF(cSize + (2 * border), border), new SizeF(width - (cSize + (3 * border)), cSize)),
                                            vehc.Name, 15, Color.White, "Arial", Game.textureCacher.LoadTexture("artwork/colours/ttdOrange.jpg"), this);

            float lbHeight = height - (2 * cSize) - (4 * border);
            windowControls[controlNum++] = new ListBox(new RectangleF(new PointF(border, cSize + (2 * border)), new SizeF(width - (2 * border), lbHeight)), 
                                            24, "", 1, Color.Black, "Arial", lbHeight / 5,  Game.textureCacher.LoadTexture("artwork/colours/ttdGrey.jpg"));
            listboxNum = controlNum - 1;

            windowControls[controlNum++] = new GameButton(new RectangleF(new PointF(border, height - cSize - border), new SizeF(bWidth, cSize)), "Skip", 11, Color.Black,
                                            "Arial", Game.textureCacher.LoadTexture("artwork/colours/ttdGrey.jpg"), Game.textureCacher.LoadTexture("artwork/colours/yellow.jpg"));
            
            windowControls[controlNum++] = new GameButton(new RectangleF(new PointF((2 * border) + (1 * bWidth), height - cSize - border), new SizeF(bWidth, cSize)), "Delete", 11, Color.Black,
                                            "Arial", Game.textureCacher.LoadTexture("artwork/colours/ttdGrey.jpg"), Game.textureCacher.LoadTexture("artwork/colours/yellow.jpg"));
            windowControls[controlNum - 1].Disabled = true;

            windowControls[controlNum++] = new GameButton(new RectangleF(new PointF((3 * border) + (2 * bWidth), height - cSize - border), new SizeF(bWidth, cSize)), "Non-Stop", 11, Color.Black,
                                            "Arial", Game.textureCacher.LoadTexture("artwork/colours/ttdGrey.jpg"), Game.textureCacher.LoadTexture("artwork/colours/yellow.jpg"));
            windowControls[controlNum - 1].Disabled = true;

            windowControls[controlNum++] = new CheckBox(new RectangleF(new PointF((4 * border) + (3 * bWidth), height - cSize - border), new SizeF(bWidth, cSize)), "GoTo", 11, Color.Black,
                                            "Arial", false, Game.textureCacher.LoadTexture("artwork/colours/ttdGrey.jpg"), Game.textureCacher.LoadTexture("artwork/colours/yellow.jpg"));
            windowControls[controlNum - 1].OnControl += new Control.ControlDelegate(SelectGoToStation);
            gotoBox = controlNum - 1;

            windowControls[controlNum++] = new GameButton(new RectangleF(new PointF((5 * border) + (4 * bWidth), height - cSize - border), new SizeF(bWidth, cSize)), "Full Load", 11, Color.Black,
                                            "Arial", Game.textureCacher.LoadTexture("artwork/colours/ttdGrey.jpg"), Game.textureCacher.LoadTexture("artwork/colours/yellow.jpg"));
            windowControls[controlNum - 1].Disabled = true;

            windowControls[controlNum++] = new GameButton(new RectangleF(new PointF((6 * border) + (5 * bWidth), height - cSize - border), new SizeF(bWidth, cSize)), "Unload", 11, Color.Black,
                                            "Arial", Game.textureCacher.LoadTexture("artwork/colours/ttdGrey.jpg"), Game.textureCacher.LoadTexture("artwork/colours/yellow.jpg"));
            windowControls[controlNum - 1].Disabled = true;

            windowControls[controlNum++] = new GameButton(new RectangleF(new PointF((7 * border) + (6 * bWidth), height - cSize - border), new SizeF(bWidth, cSize)), "Transfer", 11, Color.Black,
                                            "Arial", Game.textureCacher.LoadTexture("artwork/colours/ttdGrey.jpg"), Game.textureCacher.LoadTexture("artwork/colours/yellow.jpg"));
            windowControls[controlNum - 1].Disabled = true;

            windowControls[controlNum++] = new ResizeButton(new RectangleF(new PointF(width - cSize - border, height - border - cSize), new SizeF(cSize, cSize)),
                                                            Game.textureCacher.LoadTexture("artwork/128/resize_handle.jpg"),
                                                            Game.textureCacher.LoadTexture("artwork/128/resize_handle_down.jpg"), this);

            ListBox lb = (windowControls[listboxNum] as ListBox);
            float itemHeight = lb.ItemHeight;

            foreach (Order o in vehc.Orders)
            {
                ListableItem listItem = lb.AddItem();

                RadioButton label = new RadioButton(1, new RectangleF(new PointF(0, 0), new SizeF(listItem.Area.Width, listItem.Area.Height)), o.DestinationStation.Name, 14, Color.Black, "Arial", false, false, 
                                            Game.textureCacher.LoadTexture("artwork/colours/dark_spring_green.jpg"), Game.textureCacher.LoadTexture("artwork/colours/white.jpg"));
                listItem.AddControl(label);
            }

            //ListableItem listItem2 = lb.AddItem();
            //RadioButton label2 = new RadioButton(1, new RectangleF(new PointF(0, 0), new SizeF(listItem2.Area.Width, listItem2.Area.Height)), "- End of Orders -", 14, Color.Black, "Arial", false, false,
                                          //  Game.textureCacher.LoadTexture("artwork/colours/dark_spring_green.jpg"), Game.textureCacher.LoadTexture("artwork/colours/white.jpg"));
            //listItem2.AddControl(label2);

        }

        public override void Update()
        {
            if (Game.toolbox.IsActive(ToolType.StationSelect))
            {
                StationSelectTool tool = (StationSelectTool)Game.toolbox.GetTool(ToolType.StationSelect);
                
                Station selected = tool.Selected;

                if (selected != null && !tool.OpenWindows)
                {

                    ListBox lb = (windowControls[listboxNum] as ListBox);

                    int num = lb.SelectedItem;

                    ListableItem item = lb.AddItemAt(num);

                    Order o = new Order(selected, OrderType.None);
                    vehc.AddOrder(o);
                    
                    RadioButton label = new RadioButton(1, new RectangleF(new PointF(0, 0), new SizeF(item.Area.Width, item.Area.Height)), o.DestinationStation.Name, 14, Color.Black, "Arial", false, false,
                                            Game.textureCacher.LoadTexture("artwork/colours/dark_spring_green.jpg"), Game.textureCacher.LoadTexture("artwork/colours/white.jpg"));

                    item.AddControl(label);
                    
                    tool.Reset();

                    (windowControls[gotoBox] as CheckBox).Checked = false;
                    tool.OpenWindows = true;
                }
                else
                {

                }
            }
        }

        private void SelectGoToStation(object data, object tag)
        {
            bool selected = (bool)data;

            if (selected)
            {
                (Game.toolbox.GetTool(ToolType.StationSelect) as StationSelectTool).OpenWindows = false;
            }
            else
            {
                (Game.toolbox.GetTool(ToolType.StationSelect) as StationSelectTool).OpenWindows = true;
            }
        }

        public override void Close()
        {
            (Game.toolbox.GetTool(ToolType.StationSelect) as StationSelectTool).OpenWindows = true;
            base.Close();
        }
    }
}
